<template lang="html">
  <div class="news-edit-container">
    <el-form ref="form" :data="form" label-width="80px" class="form-container">

      <el-form-item label="新闻标题">
        <el-input v-model="form.NewsTitle" />
      </el-form-item>
      <el-form-item label="新闻内容">
        <!-- <el-input v-model="form.body" type="textarea" resize="none" :autosize="{ minRows: 5, maxRows: 10 }" /> -->
        <ItemSimditor @data="simditorData" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">发布</el-button>
      </el-form-item>

      <el-form-item label="新闻列表">
        <TableNews :loading="loading" :list="newsList" />
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import TableNews from './TableNews'
import ItemSimditor from '@/components/Common/ItemSimditor'
import { getNewsList, createNews } from '@/api/news'
export default {
  components: {
    TableNews,
    ItemSimditor
  },
  data() {
    return {
      loading: false,
      form: {
        NewsTitle: '',
        body: ''
      },
      newsList: []
    }
  },
  created() {
    this.fetchNewsList()
  },
  methods: {
    async onSubmit() {
      for (const temp in this.form) {
        if (this.form[temp] === '') {
          return this.$message.error('缺少内容')
        }
      }
      console.log('submit!', this.form)
      const params = {
        ...this.form,
        dataTime: new Date().getTime()
      }
      const result = await createNews(params)
      this.newsList.unshift({
        id: result.id,
        ...params
      })
      this.$message({
        message: `${result.message}`,
        type: 'success'
      })
    },
    async fetchNewsList() {
      this.loading = true
      const {
        data = []
      } = await getNewsList()
      this.newsList = data || []
      console.log(this.newsList)
      this.loading = false
    },
    simditorData(data) {
      console.log(data)
      this.form.body = data
    }
  }
}
</script>

<style lang="css">
.form-container {
  padding: 30px;
}
</style>
